package org.jeecg.modules.communityManagement.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.communityInfluence.entity.CommunityInfluence;
import org.jeecg.modules.communityManagement.entity.CommunityManagement;
import org.jeecg.modules.communityManagement.entity.SocietyMembersEntity;
import org.jeecg.modules.communityManagement.mapper.CommunityManagementMapper;
import org.jeecg.modules.communityManagement.service.ICommunityManagementService;
import org.jeecg.modules.ruleOfInfluence.entity.RuleOfInfluence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

/**
 * @Description: 社团管理表
 * @Author: jeecg-boot
 * @Date: 2020-07-17
 * @Version: V1.0
 */
@Service
public class CommunityManagementServiceImpl extends ServiceImpl<CommunityManagementMapper, CommunityManagement> implements ICommunityManagementService {

    @Autowired
    private CommunityManagementMapper communityManagementMapper;

    @Override
    @Transactional
    public void communityReview(CommunityManagement communityManagement) {
        communityManagement.setAudit("2");
        Boolean bool = communityManagementMapper.communityReview(communityManagement);
        if ("2".equals(communityManagement.getAudit())) {
            // 获取影响力规则
            RuleOfInfluence initialInfluence = communityManagementMapper.influenceRules();
            // 审核通过之后社团最初影响力
            String uuid = UUID.randomUUID().toString();
            uuid = uuid.replace("-", "");
            CommunityInfluence communityInfluence = new CommunityInfluence();
            communityInfluence.setId(uuid);
            communityInfluence.setClubId(communityManagement.getId());
            communityInfluence.setTotalInfluence(initialInfluence.getInitialInfluence());
            communityInfluence.setLevelTwoInfluence(initialInfluence.getLevelTwoInfluence());
            communityInfluence.setLevelThreeInfluence(initialInfluence.getLevelThreeInfluence());
            communityManagementMapper.addInitialInfluence(communityInfluence);
        }
    }

    /**
     * 社团人员新增
     *
     * @param userId
     * @param clubId
     * @return
     */
    @Override
    @Transactional
    public Boolean addSocietyMembers(String userId, String clubId) {
        Boolean bool;
        SocietyMembersEntity societyMembersEntity = new SocietyMembersEntity();
        societyMembersEntity.setId(UUIDGenerator.generate());
        societyMembersEntity.setClubId(clubId);
        societyMembersEntity.setUserId(userId);
        societyMembersEntity.setCreateTime(new Date());
        societyMembersEntity.setUpdateTime(new Date());
        // TODO 暂时使用后台用户的登陆信息，后期手机用户与后台用户区分开
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        societyMembersEntity.setCreateBy(sysUser.getUsername());
        societyMembersEntity.setUpdateBy(sysUser.getUsername());
        // 社团用户增加
        bool = communityManagementMapper.addSocietyMembers(societyMembersEntity);
        if (bool) {
            // 获取社团影响力规则
            RuleOfInfluence ruleOfInfluence = communityManagementMapper.getRuleOfInfluence();
            // 根据社团ID查询社团影响力
            CommunityInfluence communityInfluence = communityManagementMapper.getInfluenceInfo(clubId);

            int i = 0;
            // 当前社团影响力
            i = ruleOfInfluence.getAddMember() + communityInfluence.getTotalInfluence();
            communityInfluence.setTotalInfluence(i);
            // 更新社团影响力
            bool = communityManagementMapper.updateInfluence(communityInfluence);

            return bool;
        }
        return bool;
    }
}
